﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
//using System.Web.UI.WebControls;
using System.Data;
using TTSBLL;
using TTS;
using TTSModel;

public partial class T0002049_Add : System.Web.UI.Page
{
    private Assemble a;
    private string message;
    private UnitType u;
    private const string kccDownPEC = "KCQR03AA";
    private IList<UnitTypeInfo> assembleVersionListT0002048;
    private IList<UnitTypeInfo> assembleVersionListT0002049;
    private int versionNum = 3;
    private int versionNum_T0002048 = 4;
    private int versionNum_T0002049 = 3;
    private string T0002048_SN { get { return ViewState["T0002048_SN"].ToString(); } set { ViewState["T0002048_SN"] = value; } }
    private string T0002048_TestMac_SN { get { return ViewState["T0002048_TestMac_SN"].ToString(); } set { ViewState["T0002048_TestMac_SN"] = value; } }
    private string T0002048_CraftMac_SN { get { return ViewState["T0002048_CraftMac_SN"].ToString(); } set { ViewState["T0002048_CraftMac_SN"] = value; } }
    private string T0002049_SN { get { return ViewState["T0002049_SN"].ToString(); } set { ViewState["T0002049_SN"] = value; } }
    private string T0002049_KccUp_SN { get { return ViewState["T0002049_KccUp_SN"].ToString(); } set { ViewState["T0002049_KccUp_SN"] = value; } }
    private string T0002049_KccDown_SN { get { return ViewState["T0002049_KccDown_SN"].ToString(); } set { ViewState["T0002049_KccDown_SN"] = value; } }

    private DataRow dr;
    private DataTable dt
    {
        get
        {
            return (DataTable)ViewState["dt"];
        }
        set
        {
            ViewState["dt"] = value;
        }
    }

    public T0002049_Add()
    {
        a = new Assemble();
        u = new UnitType();
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt = new DataTable();
            CommonHelper.AssembleItemDataTableInitialize(dt);
            assembleVersionListT0002048 = u.LoadEntities("T0002048");
            assembleVersionListT0002049 = u.LoadEntities("T0002049");
           // this.T0002048_Version.Text = assembleVersionListT0002048[0].TcodeVersion + "," + assembleVersionListT0002048[1].TcodeVersion;
            this.T0002048_Version.Text = u.GetRandomVersion(versionNum_T0002048, "T0002048");
            //this.T0002049_Version.Text = assembleVersionListT0002049[0].TcodeVersion + "," + assembleVersionListT0002049[1].TcodeVersion;
            this.T0002049_Version.Text = u.GetRandomVersion(versionNum_T0002049, "T0002049");
            //this.T0002048_Version.Text = u.UnitType_GetbyTcode("T0002048").TcodeVersion;
            //this.T0002049_Version.Text = u.UnitType_GetbyTcode("T0002049").TcodeVersion;
        }
        this.TextBox_T0002048.Focus();
    }
    protected void Return_Click(object sender, EventArgs e)
    {
        Response.Redirect("DM.aspx");
    }
    protected void TextBox_T0002048_TextChanged(object sender, EventArgs e)
    {
        T0002048_SN = this.TextBox_T0002048.Text.Trim();
        if (SNHelper.SNCheck(T0002048_SN, "T0002048", out message))
        {          
                if (SNHelper.SNCheckVersionRandom(T0002048_SN, this.T0002048_Version.Text, out message))
                {
                    if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002048_SN, "T0002048", out message))
                    {
                        this.TextBox_T0002048.Enabled = false;
                        this.TextBox_T0002048_TestMac.Enabled = true;
                        this.TextBox_T0002048_TestMac.Focus();
                        return;
                    }
                }                      
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002048.Text = "";
        this.TextBox_T0002048.Focus();
    }
    protected void TextBox_T0002048_TestMac_TextChanged(object sender, EventArgs e)
    {
        T0002048_TestMac_SN = this.TextBox_T0002048_TestMac.Text.Trim();
        if (SNHelper.SNMacCheck(T0002048_TestMac_SN, out message))
        {
            if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002048_TestMac_SN, "T0002048", out message))
            {
                this.TextBox_T0002048_TestMac.Enabled = false;
                this.TextBox_T0002048_CraftMac.Enabled = true;
                this.TextBox_T0002048_CraftMac.Focus();
                return;
            }
        }                  
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002048_TestMac.Text = "";
        this.TextBox_T0002048_TestMac.Focus();
    }
    protected void TextBox_T0002048_CraftMac_TextChanged(object sender, EventArgs e)
    {
        T0002048_CraftMac_SN = this.TextBox_T0002048_CraftMac.Text.Trim();
        //添加比较前两个文件SN号代码
        if (T0002048_CraftMac_SN != T0002048_TestMac_SN )
        {
            if (SNHelper.SNMacCheck(T0002048_CraftMac_SN, out message))
            {
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002048_CraftMac_SN, "T0002048", out message))
                {
                    this.TextBox_T0002048_CraftMac.Enabled = false;
                    this.TextBox_T0002049.Enabled = true;
                    this.TextBox_T0002049.Focus();
                    return;
                }
            }
        }
        else
        {
            message = "T0002048 DB PCBA Craft Mac号与TT0002048 DB PCBA Test Mac号一致，请重新扫描！";
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002048_CraftMac.Text = "";
        this.TextBox_T0002048_CraftMac.Focus();
    }
    protected void TextBox_T0002049_TextChanged(object sender, EventArgs e)
    {
        T0002049_SN = this.TextBox_T0002049.Text.Trim();
        if (SNHelper.SNCheck(T0002049_SN, "T0002049", out message))
        {
            if (SNHelper.SNCheckVersionRandom(T0002049_SN, this.T0002049_Version.Text, out message))
            {
                if (ProConditionHelper.CheckMainSNsPrecondition(T0002049_SN, "T0002049", out message))
                {
                    this.TextBox_T0002049.Enabled = false;
                    this.TextBox_T0002049_KccUp.Enabled = true;
                    this.TextBox_T0002049_KccUp.Focus();
                    return;
                }
            }           
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002049.Text = "";
        this.TextBox_T0002049.Focus();
    }
    protected void TextBox_T0002049_KccUp_TextChanged(object sender, EventArgs e)
    {
        T0002049_KccUp_SN = this.TextBox_T0002049_KccUp.Text.Trim();
        if (SNHelper.KccUpSNCheck(T0002049_KccUp_SN, out message))
        {
            if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002049_KccUp_SN, out message))
            {
                this.TextBox_T0002049_KccUp.Enabled = false;
                this.TextBox_T0002049_KccDown.Enabled = true;
                this.TextBox_T0002049_KccDown.Focus();
                return;
            }
        }
                              
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002049_KccUp.Text = "";
        this.TextBox_T0002049_KccUp.Focus();
    }
    protected void TextBox_T0002049_KccDown_TextChanged(object sender, EventArgs e)
    {
        T0002049_KccDown_SN = this.TextBox_T0002049_KccDown.Text.Trim();
        if (SNHelper.KccDownSNCheck(T0002049_KccDown_SN, out message))
        {
            if (SNHelper.CheckKccDownPec(T0002049_KccDown_SN, kccDownPEC, out message))
            {
                if (SNHelper.SNCheckKccDownVersion(T0002049_KccDown_SN, "03", out message))
                {
                    this.TextBox_T0002049_KccDown.Enabled = false;
                    this.ConfirmSubmit.Enabled = true;
                    this.ConfirmSubmit.Focus();
                    return;
                }
            }
            
        }                     
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002049_KccDown.Text = "";
        this.TextBox_T0002049_KccDown.Focus();
    }



    protected void ConfirmSubmit_Click(object sender, EventArgs e)
    {
        if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002048_SN, "T0002048", out message))
        {
            if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002048_TestMac_SN, "T0002048", out message))
            {
                 if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002048_CraftMac_SN, "T0002048", out message))
                 {
                      if (ProConditionHelper.CheckMainSNsPrecondition(T0002049_SN, "T0002049", out message))
                      {
                          if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002049_KccUp_SN, out message))
                          {
                              Unit u = new Unit();
                              u.Insert(new UnitInfo() { SN = T0002048_SN, Tcode = "T0002048", IsLeaf = true, IsRoot = true });
                              u.Insert(new UnitInfo() { SN = T0002048_TestMac_SN, Tcode = "T0002048", IsLeaf = true, IsRoot = true });
                              u.Insert(new UnitInfo() { SN = T0002048_CraftMac_SN, Tcode = "T0002048", IsLeaf = true, IsRoot = true });
                              u.Insert(new UnitInfo() { SN = T0002049_SN, Tcode = "T0002049", IsLeaf = true, IsRoot = true });
                              // u.Insert(new UnitInfo() { SN = T0002049_KccUp_SN, Tcode = "T0002049", IsLeaf = true, IsRoot = true });
                              //u.Insert(new UnitInfo() { SN = T0002049_KccDown_SN, Tcode = "T0002049", IsLeaf = true, IsRoot = true });
                              KCCSNUnion ku = new KCCSNUnion();
                              KCCSNUnionInfo kui = new KCCSNUnionInfo() { KCCSN1 = T0002049_KccUp_SN, KCCSN2 = T0002049_KccDown_SN, SN = T0002049_SN, UserID = "111113" };
                              ku.Insert(kui);

                              AssembleInfo ainfo = new AssembleInfo()
                              {
                                  ID = 1,
                                  SN = T0002049_SN,
                                  IsRoot = true,
                                  RecordTime = DateTime.Now,
                                  Tcode = "T0002049",
                                  Userid = "111117"
                              };
                              dt.Rows.Clear();
                              dr = dt.NewRow();
                              dr["SubSN"] = T0002048_SN;
                              dr["Remark"] = "T0002048_DB_PCBA_SN";
                              dt.Rows.Add(dr);

                              dr = dt.NewRow();
                              dr["SubSN"] = T0002048_TestMac_SN;
                              dr["Remark"] = "T0002048_DB_PCBA_Test_Mac";
                              dt.Rows.Add(dr);

                              dr = dt.NewRow();
                              dr["SubSN"] = T0002048_CraftMac_SN;
                              dr["Remark"] = "T0002048_DB_PCBA_Craft_Mac";
                              dt.Rows.Add(dr);

                              /* dr = dt.NewRow();
                               dr["SubSN"] = T0002049_KccUp_SN;
                               dr["Remark"] = "DM_T0002049_KccUp";
                               dt.Rows.Add(dr);

                               dr = dt.NewRow();
                               dr["SubSN"] = T0002049_KccDown_SN;
                               dr["Remark"] = "DM_T0002049_KccDown";
                               dt.Rows.Add(dr);*/

                              if (a.Assemble_Insert(ainfo, dt))
                              {
                                  message = "录入成功";

                                  this.TextBox_T0002049_KccUp.Text = "";
                                  this.TextBox_T0002049_KccDown.Text = "";
                                  this.TextBox_T0002049.Text = "";
                                  this.TextBox_T0002048_TestMac.Text = "";
                                  this.TextBox_T0002048_CraftMac.Text = "";
                                  this.TextBox_T0002048.Text = "";
                                  this.TextBox_T0002048.Enabled = true;
                                  this.TextBox_T0002048.Focus();
                                  this.ConfirmSubmit.Enabled = false;

                              }
                              else
                              {
                                  message = "录入失败，请联系相关人员";
                              }
                          }
                      }
                 }
            }
        }   
       
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
    }
}